<?php
require_once '../includes/auth.php';
check_role('user'); // 仅允许普通用户访问

// 获取动态模块
$modules = $pdo->query("
    SELECT m.*, COUNT(a.id) AS access_count 
    FROM modules m
    LEFT JOIN module_access_logs a ON m.id = a.module_id
    WHERE m.is_active = 1
    GROUP BY m.id
    ORDER BY m.sort_order DESC
")->fetchAll();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据平台 - 用户首页</title>
    <link rel="stylesheet" href="../assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="../assets/css/theme.css">
    <style>
        .main-logo {
            max-width: 400px;
            margin: 2rem auto;
            transition: transform 0.3s;
        }

        .search-container {
            max-width: 800px;
            margin: 0 auto 3rem;
        }

        .module-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
            gap: 1.5rem;
            padding: 1rem;
        }

        .module-card {
            background: white;
            border: 1px solid #dee2e6;
            border-radius: 12px;
            padding: 1.5rem;
            text-align: center;
            transition: all 0.2s;
        }

        .module-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(24, 144, 255, 0.15);
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <!-- Logo -->
    <img src="../assets/images/logo.png" alt="平台Logo" class="main-logo d-block">

    <!-- 搜索框 -->
    <div class="search-container">
        <form action="search.php" method="get" class="search-form">
            <div class="input-group">
                <input type="text"
                       name="q"
                       class="form-control form-control-lg"
                       placeholder="请输入船舶名称、IMO编号或港口代码"
                       aria-label="搜索">
                <button type="submit" class="btn btn-primary btn-lg">
                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
                        <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
                    </svg>
                </button>
            </div>
        </form>
    </div>

    <!-- 动态模块加载 -->
    <div class="module-grid">
        <?php foreach ($modules as $module): ?>
            <a href="<?= htmlspecialchars($module['route_path']) ?>" class="module-card">
                <h5 class="text-primary mb-3"><?= htmlspecialchars($module['display_name']) ?></h5>
                <?php if ($module['icon_class']): ?>
                    <div class="module-icon">
                        <svg class="bi text-primary" width="40" height="40" fill="currentColor">
                            <use xlink:href="../assets/icons/bootstrap-icons.svg#<?= $module['icon_class'] ?>"/>
                        </svg>
                    </div>
                <?php endif; ?>
            </a>
        <?php endforeach; ?>
    </div>
</div>

<script src="../assets/js/bootstrap.bundle.min.js"></script>
<script src="../assets/js/modules-loader.js"></script>
</body>
</html>